package com.by.controller;

import com.by.constant.MessageConstant;
import com.by.entity.PageResult;
import com.by.entity.QueryPageBean;
import com.by.entity.Result;
import com.by.pojo.CheckGroup;
import com.by.pojo.CheckItem;
import com.by.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    /**
    * @Description: 新增检查组
            * @Param: * @param checkitemIds
     * @param checkGroup
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/add")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.add(checkitemIds, checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
    * @Description: 分页查询
            * @Param: * @param queryPageBean
            * @return: com.by.entity.PageResult
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     * @Description: 编辑检查组，回显旧数据
     * @Param: * @param id
     * @return: com.by.entity.Result
     * @Author: GP
     * @Date: 2024/1/10
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
    * @Description: 根据检查组ID来查询检查项的ID，传递给前端的编辑选项卡中的检查项Tab栏，进行回显。
            * @Param: * @param id
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkItemList = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
    * @Description: 编辑检查组
            * @Param: * @param checkitemIds
     * @param checkGroup
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/10
            */
    @RequestMapping("/edit")
    public Result eidt(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.edit(checkitemIds, checkGroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * @Description: 根据数据删除检查项
     * @Param: * @param id
     * @return: com.by.entity.Result
     * @Author: GP
     * @Date: 2024/1/10
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkGroupService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
    * @Description:查询所有检查组
            * @Param: * @param
            * @return: com.by.entity.Result
            * @Author: GP
            * @Date: 2024/1/11
            */
    @RequestMapping("/findAll")
    public Result delete(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
